package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author chencfgs
 * @date 2019-12-01 10:19
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    //@RequestParam("Ids")
    //@RequestParam("checkGroup")
    @Override
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        checkGroupDao.addGroup(checkGroup);
        Integer id = checkGroupDao.getLastId();
        for (Integer checkitemId : checkitemIds) {
            checkGroupDao.addIds(id, checkitemId);
        }
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
//        if (queryString.contains(" ") || queryString == "") {
//            queryString = null;
//        }
        if (queryString != null && queryString.length() > 0) {
            queryString = "%" + queryString + "%";
        }
        List<CheckGroup> list = checkGroupDao.selectByCondition(queryString);
        PageInfo<CheckGroup> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), list);
    }

    @Override
    public List<Integer> findItem(String id) {
        List<Integer> list = checkGroupDao.findById(id);
        return list;
    }

    @Override
    public void update(Integer[] checkitemIds, CheckGroup checkGroup) {
        Integer id = checkGroup.getId();
        System.out.println(id);
        checkGroupDao.updateGroup(checkGroup);
        checkGroupDao.delectAllIds(id);
        for (Integer checkitemId : checkitemIds) {
            checkGroupDao.addIds(id, checkitemId);
        }
    }

    @Override
    public Long findById(Integer id) {
        Long aLong = checkGroupDao.findcheckitemById(id);
        return aLong;
    }

    @Override
    public void deleteById(Integer id) {
        checkGroupDao.delectGroupById(id);
    }

    @Override
    public List<CheckGroup> findAllGroup() {
        List<CheckGroup> list = checkGroupDao.findAll();
        return list;
    }
}
